feat: add extra observability metrics for PrometheusMiddleware and Receiver#604
Conversation
…tamp to PrometheusMiddleware
Add production observability for the Receiver via an observer protocol that tracks prefetch queue depth, semaphore availability, active task count, unknown task lookups, and deserialization errors. - Add ReceiverObserver protocol (taskiq/receiver/observer.py) - Instrument Receiver with guarded observer callbacks at 5 sites - Add PrometheusReceiverObserver implementation with Gauges/Counters - Wire observer from middleware to receiver via broker attribute - Remove redundant in_flight_tasks gauge (replaced by active_tasks_count)
- Fix typos in observer docstring and metric descriptions - Add missing docstrings to observer protocol and implementation methods - Remove unused Gauge import from PrometheusMiddleware.__init__ - Remove unused ReceiverObserver import from run.py - Fix import ordering (ruff I001) - Add noqa for expected complexity in runner method - Run black formatting
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #604 +/- ##
==========================================
- Coverage 77.70% 77.67% -0.03%
==========================================
Files 62 69 +7
Lines 1933 2477 +544
==========================================
+ Hits 1502 1924 +422
- Misses 431 553 +122 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Can we get a review? |
|
This one does look good. Thank for your contribution. But can you please rebase onto master branch? |
|
Ah of course, I will rebase and edit. And btw, is there a roadmap for what is coming? if so I would like to help. |
Summary
queue_wait_secondshistogram (end-to-end queue latency via_taskiq_enqueue_timestamplabel injected inpre_send),task_errors_by_typecounter(error count broken down by exception class name), and idempotent timestamp injection so multiple middlewares can coexist
typing.Protocol(taskiq/receiver/observer.py) that lets external implementations observe receiver internals — prefetch queue depth, semaphoreavailability, active task count, unknown task lookups, and deserialization errors — with zero overhead when no observer is set (
if self.observer is not Noneguards)PrometheusMiddleware.set_broker()→broker._receiver_observer→Receiver(observer=...)New metrics
queue_wait_secondstask_errors_by_typetask_name,error_type)prefetch_queue_sizesemaphore_availableworker_active_tasks_counttask_not_found_totaltask_name)deserialize_error_countSome decisions decisions
ReceiverObserveris aProtocol(not ABC) so there's no coupling — any class with matching methods satisfies itqueue_wait_secondsgracefully degrades: if the sender doesn't usePrometheusMiddleware, the timestamp label is absent and the metric is simply not observedTesting
Honestly still no idea how to test this via py test. But I'll be working on it.